import defineReactive from './defineReactive';
import { proxyPrototype } from './array';
import Dep from './Dep';
import { def } from './utils';
import observe from './observe';

// 为对象对每个属性设置响应式
export default class Observer {
  constructor(value) {
    this.value = value;
    this.dep = new Dep();
    def(value, '__ob__', this);
    if (Array.isArray(value)) {
      // 设置数组的原型
      Object.setPrototypeOf(value, proxyPrototype);
      this.observeArray(value);
    } else {
      this.walk(value);
    }
  }

  walk(obj) {
    Object.keys(obj).forEach((key) => defineReactive(obj, key, obj[key]));
  }
  // 需要继续监听数组内的元素(如果数组元素是对象的话)
  observeArray(arr) {
    arr.forEach((i) => observe(i));
  }
}
